package com.cloudwise.controller;

import com.cloudwise.entity.PageBean;
import com.cloudwise.entity.ReturnBean;
import com.cloudwise.entity.Userinfo;
import com.cloudwise.service.UserinfoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Userinfo)表控制层
 *
 * @author makejava
 * @since 2020-12-14 09:40:31
 * RestController=(controller+responseBody)
 */
@RestController
//窄化请求==》支持多个controller中@GetMapping地址相同
@RequestMapping("userinfo")
public class UserinfoController  extends  BaseController {
    /**
     * 服务对象
     */
    @Resource
    private UserinfoService userinfoService;

    /**
     * 通过主键查询单条数据
     *params = "id",要求前端传入的参数必须包含id，如果没有id，报错
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "selectOne",params = "id")
    public Userinfo selectOne(Integer id) {
        return this.userinfoService.queryById(id);
    }
    /**
     * @create by: Teacher陈
     * @description: 条件查询加分页
     * @create time: 2020/12/15 16:39
     * @return
     */
    @GetMapping(value = "selectAll")
    public  ReturnBean selectAll(PageBean pageBean,Userinfo userinfo) {
        //计算分页参数
        int offset = calcPageParam(pageBean);
        List<Userinfo> userinfoList = userinfoService.queryAllByConditionAndLimit(userinfo,offset, pageBean.getLimit());
        long count = userinfoService.getCount(userinfo);
        //返回json结果
        ReturnBean returnBean = returnSuccess(userinfoList);
        returnBean.setCount(count);
        return returnBean;
    }
/**
 * @create by: Teacher陈
 * @description: 保存用户
 * @create time: 2020/12/16 10:06
 * @param userinfo
 * @return ReturnBean
 */
    @PostMapping("saveUser")
    public  ReturnBean saveUser(Userinfo userinfo){
        try {
            userinfo.setCreatetime(new Date());
            Userinfo user = userinfoService.insert(userinfo);
            return returnSuccess(user);
        }catch (Exception e){
            return returnFail(null);
        }
    }
    /**
     * @create by: Teacher陈
     * @description: 按照主键id删除
     * @create time: 2020/12/16 10:10
     * @param id
     * @return ReturnBean
     */
    @DeleteMapping(value = "deleteUserById",params = "id")
    public  ReturnBean deleteUserById(Integer id){

        boolean delete = userinfoService.deleteById(id);
        if(delete){
          return   returnSuccess(null);
        }
        return returnFail(null);

    }
    @RequestMapping("deleteUserByIds")
    public  ReturnBean deleteUserByIds( String  ids){

        boolean delete = userinfoService.deleteByIds(ids);
        if(delete){
          return   returnSuccess(null);
        }
        return returnFail(null);

    }
    /**
     * @create by: Teacher陈
     * @description: 修改用户
     * @create time: 2020/12/15 11:36
     * @param userinfo
     * @return  ReturnBean
     */
    @PutMapping("updateUser")
    public  ReturnBean updateUser(Userinfo userinfo){
        try {
            userinfo.setCreatetime(new Date());
            Userinfo user = userinfoService.update(userinfo);
            return returnSuccess(user);
        }catch (Exception e){
            return returnFail(null);
        }
    }
}